from celery import shared_task
from datetime import datetime, timedelta
from services.stats_service import StatsService

@shared_task
def run_daily_stats():
    """执行日统计（统计前一天数据）"""
    yesterday = datetime.now().date() - timedelta(days=1)
    StatsService.run_stats(period=1, stats_date=yesterday)

@shared_task
def run_weekly_stats():
    """执行周统计（统计上一周数据，周日为统计日期）"""
    last_sunday = datetime.now().date() - timedelta(days=datetime.now().weekday() + 1)
    StatsService.run_stats(period=2, stats_date=last_sunday)

@shared_task
def run_monthly_stats():
    """执行月统计（统计上一个月数据）"""
    # 获取上一个月的最后一天
    today = datetime.now().date()
    last_month_end = (today.replace(day=1) - timedelta(days=1)).date()
    StatsService.run_stats(period=3, stats_date=last_month_end)